# -*- coding: utf-8 -*- 
"""
登录窗口，采用QDialog实现
"""
import sys
import os
path = os.path.normpath(os.path.join(os.getcwd(), os.path.pardir))
sys.path.append(path)
from PyQt5.QtWidgets import QDialog, QLabel, QLineEdit, QPushButton, QHBoxLayout
from PyQt5.QtGui import QIcon, QPixmap, QPalette, QBrush
from PyQt5.QtCore import QSize, QRect, pyqtSignal, pyqtSlot
from PyQt5.Qt import Qt
from PyQt5.QtWidgets import QApplication
from conf import APPConfig
from ui.customQtEditor.labelButton import labelButton
from resources import qrc_resources

#引入自定义库
from db.connect.dbConnection import dbCon
from bmlog.bmLogger import bmLogger

class loginWindow(QDialog):
    """
    自定义登录窗口
    """
    def __init__(self):
        self.logger = bmLogger.getInstance().bmloger
        super(loginWindow, self).__init__()
        self.logger.info(str(self.__class__)+("初始化"))
        #定义对话框外观模式
        #self.setWindowFlags(Qt.FramelessWindowHint)
        self.setWindowTitle(APPConfig.LOGIN_TITLE)
        #设置登录窗口大小与位置
        self.setFixedSize(QSize(347, 264))
        desktop = QApplication.desktop()
        width = desktop.width()
        height = desktop.height()
        self.move((width - self.width()) / 2, (height - self.height()) / 2)
        #设置窗口透明度
        self.setWindowOpacity(1)
        #初始化登录窗口布局
        #设置窗口背景
        icon = QIcon(QPixmap(":img/images/play.jpg"))
        self.setWindowIcon(icon)
        #初始化布局控件
        self.initEditors()

        

    def initEditors(self):
        """
        定义控件，初始化用户名、密码输入框
        背景图等
        """
        #背景图
        backPalete = QPalette()
        backPalete.setBrush(self.backgroundRole(), QBrush(QPixmap(":/img/images/bamboo.jpg")))
        self.setPalette(backPalete)
        self.setWindowFlags(Qt.FramelessWindowHint)

        #最小化按钮
        minBtn = QPushButton(self)
        minBtn.setText("-")
        minBtn.setGeometry(QRect(302, 1, 18, 18))

        closeBtn = QPushButton(self)
        closeBtn.setText("x")
        closeBtn.setGeometry(QRect(320, 1, 18, 18))
        #用户名、密码
        label_user = QLabel(u"账号：",self)
        label_user.setGeometry(QRect(80, 80, 50, 22))
        label_passwd = QLabel(u"密码：",self)
        label_passwd.setGeometry(QRect(80, 120, 50, 22))

        self.lineEdit_user = QLineEdit(u"root",self)
        self.lineEdit_user.setGeometry(QRect(120, 80, 150, 22))
        self.lineEdit_pass = QLineEdit("",self)
        self.lineEdit_pass.setEchoMode(QLineEdit.Password)
        self.lineEdit_pass.setGeometry(QRect(120, 120, 150, 22))
        #按钮
        loginBtn = QPushButton(u"登录", self)
        loginBtn.setGeometry(QRect(100, 150, 60, 22))

        restBtn = QPushButton(u"重置", self)
        restBtn.setGeometry(QRect(210, 150, 60, 22))
        #绑定信号
        minBtn.clicked.connect(self.minLoginWindow)
        closeBtn.clicked.connect(self.closeLoginWindow)
        loginBtn.clicked.connect(self.doLogin)
        restBtn.clicked.connect(self.restLoginInfo)

    def minLoginWindow(self):
        """
        最小化登录窗口
        """
        self.showMinimized()

    def closeLoginWindow(self):
        """
        取消按钮事件
        """
        self.reject()


    def doLogin(self):
        """
        登录按钮事件
        """
        username = self.lineEdit_user.text()
        password = self.lineEdit_pass.text()

        db = dbCon();
        #\　换行符
        result = db.findObject("bm_pub_user", {"login_code": username, "login_pass": password})
        if (result is None):
            print("errrot")
        else:
            self.accept()

    def restLoginInfo(self):
        """
        重置登录用记与密码
        """
        self.lineEdit_user.setText("")
        self.lineEdit_pass.setText("")
